package com.iweipan.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.iweipan.R;
import com.iweipan.adapter.MyQuanListAdapter;
import com.iweipan.base.WPBaseFragment;
import com.iweipan.bean.MyQuanListBean;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import okhttp3.Call;

import static com.iweipan.base.WPUrlBase.WP_USER_DETAIL;

/**
 * Created by xyb on 2016/12/21.
 */

public class MyQuanListFragment extends WPBaseFragment {


    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.error)
    TextView error;
    @BindView(R.id.twinklingRefreshLayout)
    TwinklingRefreshLayout twinklingRefreshLayout;
    private int pageNo;
    private int pageSize;
    private int type;
    private String token;
    private MyQuanListAdapter myQuanListAdapter;

    public static MyQuanListFragment newInstance(int type, String token) {
        Bundle args = new Bundle();
        MyQuanListFragment fragment = new MyQuanListFragment();
        args.putInt("type", type);
        args.putString("token", token);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initData() {

        myQuanListAdapter = new MyQuanListAdapter(mActivity);
//        listView.setEmptyView(error);
        listView.setAdapter(myQuanListAdapter);
        Bundle arguments = getArguments();
        type = arguments.getInt("type");
        token = arguments.getString("token");
        initHttp(type, false);
        twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                initHttp(type, false);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                initHttp(type, true);
            }
        });
    }

    private void initHttp(int type, boolean isLoad) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("token", token);
            if (!isLoad) {
                pageNo = 0;
                pageSize = 100;
            } else {
                pageNo = 0;
                pageSize += 100;
            }

            jsonObject.put("pageNo", pageNo);
            jsonObject.put("pageSize", pageSize);

            OkHttpUtils.get()
                    .addParams("p", jsonObject.toString())
                    .url(WP_USER_DETAIL)
                    .build()
                    .execute(new MyStringCallBack(type, isLoad));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private class MyStringCallBack extends StringCallback {
        private final boolean isLoad;
        private int type;

        public MyStringCallBack(int type, boolean isLoad) {
            this.type = type;
            this.isLoad = isLoad;
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(String response, int id) {
//            Log.e("MyStringCallBack", response);
            if (isLoad) {
                twinklingRefreshLayout.finishLoadmore();
            } else {
                twinklingRefreshLayout.finishRefreshing();
            }
            try {
                JSONObject jsonObject = new JSONObject(response);
                String code = jsonObject.getString("code");
                if (code.equals("200")) {
                    ArrayList<MyQuanListBean.ResultBean> myQuanLists = new ArrayList<>();
                    JSONArray result = jsonObject.getJSONArray("result");
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject resultJSONObject = result.getJSONObject(i);
                        int couponId = resultJSONObject.getInt("couponId");
                        String couponName = resultJSONObject.getString("couponName");
                        String endTime = resultJSONObject.getString("endTime");
                        String startTime = resultJSONObject.getString("startTime");
                        switch (type) {
                            case 0:
                                if (couponId == 1) {
                                    MyQuanListBean.ResultBean resultBean = new MyQuanListBean.ResultBean();
                                    resultBean.setCouponId(couponId);
                                    resultBean.setCouponName(couponName);
                                    resultBean.setEndTime(endTime);
                                    resultBean.setStartTime(startTime);
                                    myQuanLists.add(resultBean);
                                }
                                break;
                            case 1:
                                if (couponId == 2) {
                                    MyQuanListBean.ResultBean resultBean = new MyQuanListBean.ResultBean();
                                    resultBean.setCouponId(couponId);
                                    resultBean.setCouponName(couponName);
                                    resultBean.setEndTime(endTime);
                                    resultBean.setStartTime(startTime);
                                    myQuanLists.add(resultBean);
                                }
                                break;
                            case 2:
                                if (couponId == 3) {
                                    MyQuanListBean.ResultBean resultBean = new MyQuanListBean.ResultBean();
                                    resultBean.setCouponId(couponId);
                                    resultBean.setCouponName(couponName);
                                    resultBean.setEndTime(endTime);
                                    resultBean.setStartTime(startTime);
                                    myQuanLists.add(resultBean);
                                }
                                break;
                        }


                    }
                    myQuanListAdapter.addLists(myQuanLists);
                    int count = listView.getCount();
                    if (count > 0) {
                        error.setVisibility(View.GONE);
                    } else {
                        error.setVisibility(View.VISIBLE);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void initDestrory() {

    }

    @Override
    public void initView() {

    }

    @Override
    public View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_myquan, null);
        return view;
    }

}
